package samfeng.erp.quant.service;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import samfeng.erp.location.dal.dataobject.LocationDO;
import samfeng.erp.location.dal.mysql.LocationMapper;
import samfeng.erp.quant.controller.admin.vo.*;
import samfeng.erp.quant.convert.QuantConvert;
import samfeng.erp.quant.dal.dataobject.QuantDO;
import samfeng.erp.quant.dal.mysql.QuantMapper;

import java.util.*;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static samfeng.erp.quant.enmus.ErrorCodeConstants.QUANT_ERROR;

/**
 * 仓库计算记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class QuantServiceImpl implements QuantService {

    @Resource
    private QuantMapper quantMapper;

    @Resource
    private LocationMapper locationMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createQuant(QuantCreateReqVO createReqVO) {
        // 插入
        QuantDO quant = QuantConvert.INSTANCE.convert(createReqVO);
        quantMapper.insert(quant);
        // 返回
        return quant.getId();
    }

    @Override
    public void updateQuant(QuantUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateQuantExists(updateReqVO.getId());
        // 更新
        QuantDO updateObj = QuantConvert.INSTANCE.convert(updateReqVO);
        quantMapper.updateById(updateObj);
    }

    @Override
    public void deleteQuant(Long id) {
        // 校验存在
        this.validateQuantExists(id);
        // 删除
        quantMapper.deleteById(id);
    }

    private void validateQuantExists(Long id) {
        if (quantMapper.selectById(id) == null) {
            throw exception(QUANT_ERROR);
        }
    }

    @Override
    public QuantDO getQuant(Long id) {
        return quantMapper.selectById(id);
    }

    @Override
    public List<QuantDO> getQuantList(Collection<Long> ids) {
        return quantMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<QuantDO> getQuantPage(QuantPageReqVO pageReqVO) {
        return quantMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<QuantDO> getQuantPageInProductPage(QuantPageReqVO pageReqVO) {
        PageResult<QuantDO> quantDopPageResult = quantMapper.selectPage(pageReqVO, new LambdaQueryWrapperX<QuantDO>()
                .eq(QuantDO::getProductId, pageReqVO.getProductId()));
        List<QuantDO> quantDos = quantDopPageResult.getList();
        quantDos.forEach(quantDO -> {
            LocationDO locationDO = locationMapper.selectOne("id", quantDO.getLocationId());
            quantDO.setLocationName(locationDO.getName());
        });
        return quantDopPageResult;
    }

    @Override
    public PageResult<StockQuantSummarizingPageReqVO> getStockQuantSummarizingPageVO(StockQuantSummarizingPageReqVO stockQuantSummarizingPageReqVO) {
        return null;
    }

    @Override
    public List<QuantDO> getQuantList(QuantExportReqVO exportReqVO) {
        return quantMapper.selectList(exportReqVO);
    }

}
